import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dwh5/login/input.dart';
import 'package:flutter_dwh5/utils/api.dart';
import 'package:flutter_dwh5/utils/dio_http.dart';
import 'package:fluttertoast/fluttertoast.dart';

class Login extends StatefulWidget {
  @override
  State<Login> createState() => _LoginState();
}

class _LoginState extends State<Login> {
  String mobile;
  String password;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          '登录',
          style: TextStyle(color: Colors.black),
        ),
        backgroundColor: Color.fromRGBO(242, 242, 242, 1),
        centerTitle: true,
        elevation: 0,
        //leading: Icon(Icons.close),
      ),
      body: Container(
        padding: EdgeInsets.all(30),
        child: Column(
          children: [
            SizedBox(
              height: 40,
            ),
            Image.asset(
              'images/login/logo.png',
              width: 103,
              height: 71,
            ),
            Input(callBackMobile: (mobile) {
              setState(() {
                this.mobile = mobile;
              });
            }, callBackPassword: (password) {
              setState(() {
                this.password = password;
              });
            }),
            Container(
              margin: EdgeInsets.only(top: 20),
              width: MediaQuery.of(context).size.width,
              height: 38,
              child: ElevatedButton(
                onPressed: () async {
                  var res = await Api.LoginAPI(
                      {'mobile': this.mobile, 'password': this.password});

                  if (res['code'] == 1) {
                    Fluttertoast.showToast(msg: res['msg']);
                    Navigator.pushNamed(context, 'my');
                  } else {
                    Fluttertoast.showToast(msg: res['msg']);
                  }
                },
                child: Text(
                  '确认登录',
                  style: TextStyle(color: Colors.white),
                ),
                // style: ElevatedButton.styleFrom(backgroundColor: Colors.black),
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 15),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(5),
                  color: Color.fromRGBO(85, 169, 71, 1)),
              height: 38,
              width: MediaQuery.of(context).size.width,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  IconButton(
                    onPressed: () {},
                    icon: Image.asset(
                      'images/login/wechat.png',
                      width: 21,
                      height: 18,
                    ),
                  ),
                  Text(
                    '微信登录',
                    style: TextStyle(color: Colors.white),
                  )
                ],
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                TextButton(
                  onPressed: () {
                    Navigator.pushNamed(context, 'setpsd');
                  },
                  child: Text(
                    '忘记密码',
                    style: TextStyle(color: Colors.black),
                  ),
                ),
                TextButton(
                    onPressed: () {
                      Navigator.pushReplacementNamed(context, 'register');
                    },
                    child: Text(
                      '立即注册',
                      style: TextStyle(
                          color: Colors.black,
                          decoration: TextDecoration.underline,
                          fontSize: 14),
                    ))
              ],
            )
          ],
        ),
      ),
    );
  }
}
